package com.ym.medical.service.impl;

import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.StandardDeptDao;
import com.ym.medical.domain.entity.StandardDeptEntity;
import com.ym.medical.domain.param.StandardDeptQueryParam;
import com.ym.medical.domain.param.StandardDeptUpdateParam;
import com.ym.medical.domain.vo.StandardDeptListPcVO;
import com.ym.medical.service.IStandardDeptSV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 标准科室Service业务层处理
*
* @author ym
* @date 2020-08-25
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class StandardDeptSVImpl extends BaseSVImpl<StandardDeptEntity,StandardDeptDao> implements IStandardDeptSV {

    private static Logger logger = LoggerFactory.getLogger(StandardDeptSVImpl.class);

    /**
     * 标准科室列表
     * @param queryParam model 对象
     * @return
     */
    @Override
    public List<StandardDeptListPcVO> selectListPC(StandardDeptQueryParam queryParam) {
        List<StandardDeptListPcVO> standardDeptListPcVOS = mapper.selectListPC(queryParam);
        return standardDeptListPcVOS;
    }


    /**
     * 所有科室名称
     * @return
     */
    @Override
    public List<StandardDeptEntity> selectSDeptName() {
        List<StandardDeptEntity> standardDepts = mapper.selectSDeptName();
        return standardDepts;
    }

    /**
     * 新增标准科室
     * @param updateParam
     * @return
     */
    @Override
    public Long addSdept(StandardDeptUpdateParam updateParam) {
        if (null == updateParam){
            throw new BusinessException("新增对象不能为空");
        }
        if (StringUtils.isEmpty(updateParam.getDeptCode())){
            throw new BusinessException("编码不能为空");
        }
        if (StringUtils.isEmpty(updateParam.getDeptName())){
            throw new BusinessException("科室名能为空");
        }

        Long id = null;
        try {
            StandardDeptEntity deptEntity = updateParam.covert();
            id = insert(deptEntity);
        } catch (Exception e) {
            logger.info("StandardDeptSVImpl addSdept",e.getMessage());
            if (e instanceof DuplicateKeyException){
                throw new BusinessException("科室编码重复");
            }
        }
        return id;
    }

    /**
     * 标准科室更改
     * @param updateParam
     * @return
     */
    @Override
    public int updateSdept(StandardDeptUpdateParam updateParam) {
        StandardDeptEntity deptEntity = updateParam.covert();
        int rows = updateById(deptEntity);
        return rows;
    }
}
